package com.fragrance.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fragrance.util.Constants;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * 设备信息表 t_device_main
 * 
 * @author zhilin
 *
 */
public class DeviceMainModel extends Model<DeviceMainModel> {

	private static final long serialVersionUID = 2006329684172642033L;

	public final static DeviceMainModel me = new DeviceMainModel();

	private final static Logger logger = LoggerFactory.getLogger(DeviceMainModel.class);

	/**
	 * 根据imei户
	 * 
	 * @param imei设备信息
	 * @return
	 */
	public List<DeviceMainModel> findDevice(String imei) {
		// right_status为零时说明机器可以使用
		return find("select * from t_device_main where imei = ? and right_status = 0 ", imei);
	}

	/**
	 * 根据imei户
	 * 
	 * @param imei设备信息
	 * @return
	 */
	public List<DeviceMainModel> findDeviceByImei(String imei) {
		// right_status为零时说明机器可以使用
		return find("select * from t_device_main where status = 'N' and imei = ?  ", imei);
	}

	public List<DeviceMainModel> checkDevice(String imei, String device_status) {
		// right_status为零时说明机器可以使用
		return find("select * from t_device_main where status = 'N' and imei = ? and right_status = ? ",//
				imei,
				device_status);
	}

	public boolean updateStatus(Integer newDeviceStatus, String imei, Integer oldDeviceStatus) {
		boolean flag = false;
		List<DeviceMainModel> list = this.find("select * from t_device_main where imei=?  " //
				+ " and device_status = ? ", imei,
				oldDeviceStatus);
		if (list != null && list.size() > 0) {
			return list.get(0).set("device_status", newDeviceStatus).update();
		}
		return flag;
	}

	// 通过simno和imei判断设备是否存在
	public boolean deviceIsExist(String simNo, String imei) {
		boolean flag = false;
		List<DeviceMainModel> list = this.find("select * from t_device_main where imei = ? or sim_no = ? "//
				, imei,
				simNo);
		if (list != null && list.size() > 0) {
			flag = true;
		}
		return flag;
	}
	
	//赋予设备多台绑定的权限
	public Map<String, Object> deviceBindRight(Map<String, Object> params) {
		logger.info("---- begin deviceMainModel deviceBindRight --- params{}",params);
		String imei = (String) params.get("IMEI");
		String remark1 = (String) params.get("remark1");
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			boolean flag = this.find("select * from t_device_main where imei = ? and status = 'N' " ,imei)
					.get(0)
					.set("remark1", remark1)
					.update();
			if(flag){
				resultMap.put("code", Constants.SUCCESS_CODE);
				resultMap.put("msg", Constants.SUCCESS_MSG);
			}
			
		} catch (Exception e) {
			logger.error("-- error deviceMainModel deviceBindRight,Exception{}", e);
			resultMap.put("code", "300001");
			resultMap.put("msg", "设备绑定权限异常");
		}
		logger.info("---- end deviceMainModel deviceBindRight --- ");
		return resultMap;
	}
	
	

}
